package com.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.EasyuiUtils;
import com.common.JsonTreeData;
import com.dao.sys.SysManageMapper;

@Controller
public class SysController {
	static final Logger logger = LoggerFactory.getLogger(SysController.class);

	@Autowired
	private SysManageMapper sysManageMapper;

	@RequestMapping(value = "/sys/selectUsers", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectUsers(HttpServletRequest request) {
		try {
			HashMap<String, Object> hashtable = new HashMap<>();
			String queryName = request.getParameter("queryName");
			if (queryName != null && !queryName.equals("")) {
				hashtable.put("queryName", queryName);
			}
			List<HashMap<String, Object>> list = sysManageMapper.selectUsers(hashtable);
			String jsonString = JSON.toJSONString(list);
//			jsonString = EasyuiUtils.getDatagridPageJson(jsonString, taskMapper.selectCount(hashtable));
			logger.info("selectUsers:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectUsers:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/sys/selectModuleMap", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectModuleMap(HttpServletRequest request) {
		try {
			String userid = (String) request.getSession().getAttribute("display_userid");
			logger.debug("userid:{}", userid);
			List<HashMap<String, Object>> list = sysManageMapper.selectModuleMap(userid);
			List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
			for (Map<String, Object> map : list) {
				logger.info("moduleid:{}", map.get("moduleid"));
				JsonTreeData treeData = new JsonTreeData();
				treeData.setId(map.get("moduleid").toString());
				treeData.setPid(map.get("PARENTMODULEID".toLowerCase()).toString());
				treeData.setText(map.get("MODULENAME".toLowerCase()).toString());
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", map.get("MODULEURL".toLowerCase()).toString());
				treeData.setAttributes(attributes);
				treeDataList.add(treeData);

			}
			List<JsonTreeData> newTreeDataList = EasyuiUtils.getTreeJson(treeDataList, "0", "");

			// JSONArray jsonarray = JSONArray.fromObject(newTreeDataList);
			String slist = JSON.toJSONString(newTreeDataList);
			// System.out.println(slist);
			logger.info("treelist:{}", list);
			// request.setAttribute("addLists", list);
			return slist;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return "test/test";
		}
	}

}
